package com.huabo.audit.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.oracle.entity.TblNbsjAdvicenoteEntity;
import com.huabo.audit.oracle.entity.TblNbsjProposalEntity;
import com.huabo.audit.oracle.entity.TblReportEntity;
import com.huabo.audit.oracle.vo.TblNbsjProposalVo;
import com.huabo.audit.oracle.vo.TblReportVo;
import com.huabo.audit.service.AttachmentService;
import com.huabo.audit.service.TblAttachmentService;
import com.huabo.audit.service.TblNbsjAdvicenoteService;
import com.huabo.audit.service.TblNbsjOperateService;
import com.huabo.audit.service.TblNbsjProjectService;
import com.huabo.audit.service.TblNbsjProposalService;
import com.huabo.audit.service.TblNbsjSheetReportService;
import com.huabo.audit.service.TblNbsjSheetService;
import com.huabo.audit.service.TblReportService;
import com.huabo.audit.service.ZhAssessService;
import com.huabo.audit.service.ZhContentService;
import com.huabo.audit.service.ZhFormService;
import com.huabo.audit.util.DateUtils;
import com.huabo.audit.util.FileUtil;
import com.huabo.audit.util.FreeMarkerUtil;
import com.huabo.audit.util.MD5Encrypt;
import com.huabo.audit.util.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 审计报告阶段
*/
@RestController
@Slf4j
@Api(value = "审计报告阶段", tags = {"审计报告阶段接口"})
@RequestMapping(value = "/auditReport")
public class NbsjReportController {
	
	@Resource
	public TblNbsjProjectService tblnbsjProjectService;
	
	@Resource
	public TblNbsjProposalService tblNbsjProposalService;
	
	@Resource
	public AttachmentService attachmentService;
	
	@Resource
	public ZhFormService zhFormService;
	
	@Resource
	public ZhContentService zhContentService;
	
	@Resource
	public ZhAssessService zhAssessService;
	
	@Resource
	public TblNbsjSheetReportService tblNbsjSheetReportService;
	
	@Resource
	public TblNbsjSheetService tBlNbsjSheetService;
	
	@Resource
	public TblNbsjOperateService tblNbsjOperateService;
	
	@Resource
	public TblNbsjAdvicenoteService tblNbsjAdvicenoteService;
	
	@Resource
	public TblReportService tblReportService;

	@Autowired
	public FreeMarkerConfig freeMarkerConfig;
	
	@Resource
	private TblAttachmentService tblAttachmentService;
	
	/**
	 * 审计报告编制列表
	 * */
	@GetMapping("/manage/report")
	@ApiOperation(value = "审计报告编制列表")
	public JsonBean in_meet_record_list(HttpServletRequest request, TblReportVo tblReportVo,
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
			@ApiParam(name = "projectId", value = "", required = false) @RequestParam(value = "projectId", required = false) Integer projectId) {

		JsonBean jsonBean = null;
		try {
//			jsonBean = this.zhFormService.reportPageList(token, pageNumber, pageSize,zhFormVo);
			tblReportVo.setType("nbsj");
			
			jsonBean = this.tblReportService.zdyPageList(token, pageNumber, pageSize, tblReportVo,projectId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;

	}
	/**
	 * 审计报告编制-新增与修改
	 */
	@RequestMapping(value = "/manage/report_add", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计报告编制-新增与修改")
    public JsonBean workReportSave(HttpServletRequest request,@ApiParam(name="zf",value="实体",required=true) TblReportEntity report,
			 						@ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
								   @ApiParam(name="attids",value="附件id数组",required=false)String attids)throws Exception{
		JsonBean jsonBean = null;
		try {
//			jsonBean = this.zhFormService.reportAdd(zf,token);
			report.setType("nbsj");
			jsonBean = this.tblReportService.zdyAdd(report, token,attids);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	/**
	 * 审计报告编制-附件列表
	 */
	@GetMapping("/manage/report_file_list")
	@ApiOperation(value = "审计报告编制-附件列表")
	public JsonBean report_file_list(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "reportid", value = "业务主键", required = true) @RequestParam(value = "reportid", required = true) Integer reportid) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblAttachmentService.reportFileList(token,reportid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
     * 审计报告编制-附件删除
     */
    @GetMapping("/manage/report_file_del")
    @ApiOperation(value = "审计报告编制-附件删除")
    public R report_file_del(HttpServletRequest request, HttpServletResponse response,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
    	return this.tblReportService.removeAttInfoByAttId(token, attId);
    }
	
	/**
	 * 审计报告编制-删除
	 */
	@GetMapping("/manage/report_del")
	@ApiOperation(value = "审计报告编制-删除")
    public JsonBean workReportDelete(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="reportid",value="主键",required=true)@RequestParam(value = "reportid", required = true) Integer reportid) {
        
        try {
//			return zhFormService.reportDelete(formid, token);
        	return tblReportService.zdyDelete(reportid, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	/**
	 * 审计报告编制-明细
	 */
	@GetMapping("/manage/report_detail")
    @ApiOperation(value = "审计报告编制-明细")
    public JsonBean workReportDetail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="reportid",value="主键",required=true)@RequestParam(value = "reportid", required = true) Integer reportid) {
    	JsonBean jsonBean = null;
    	try {
//			jsonBean = zhFormService.findNbsjWorkReportDetail(token,formid);
    		jsonBean = tblReportService.findZdyReportDetail(token,reportid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }

	/**
	 * 报告编制-导出
	 */
	@GetMapping("/manage/report_export")
	@ApiOperation(value = "报告编制-导出")
	public void in_meet_record_export(HttpServletRequest request, HttpServletResponse response,
									  @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
									  @ApiParam(name="reportid",value="报告编制主键",required=true)@RequestParam(value = "reportid", required = true) String reportid) throws Exception {
		Map<String, String> map = new HashMap<String,String>();
		TblReportEntity info= tblReportService.findReportByreportId(reportid);
		map.put("repdesc", info.getRepdesc());
		String fileName =info.getReportname()+".doc";
		String FREEMARKER_PATH = request.getSession().getServletContext().getRealPath("/template/doc");
		Boolean flag =(Boolean)FreeMarkerUtil.htmlFileHasExist(request, FREEMARKER_PATH, fileName).get("exist");
		if(!flag){//如何静态文件不存在，重新生成
			FreeMarkerUtil.createHtml(freeMarkerConfig, "static.ftl", request, map, FREEMARKER_PATH, fileName);//根据模板生成静态页面
		}
		String fileName1 = FREEMARKER_PATH +"/"+ fileName;
		FileUtil.downLoad(fileName1, response, false, fileName);
		FileUtil.deleteFile(fileName);
	}


	/**
	 * 审计建议书列表
	 * */
	@GetMapping("/sjzj/audit_suggest_list")
	@ApiOperation(value = "审计建议书列表")
	public JsonBean audit_suggest_list(HttpServletRequest request, TblNbsjProposalVo tblNbsjProposalVo,
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
			@ApiParam(name = "projectId", value = "", required = false) @RequestParam(value = "projectId", required = false) Integer projectId) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjProposalService.suggestPageList(token, pageNumber, pageSize,tblNbsjProposalVo,projectId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;

	}
	/**
	 * 审计建议书-新增与修改
	 */
	@RequestMapping(value = "/sjzj/audit_suggest_add", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计建议书-新增与修改")
    public JsonBean audit_suggest_add(HttpServletRequest request,@ApiParam(name="proposal",value="实体",required=true) TblNbsjProposalEntity proposal,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="attids",value="附件id数组",required=false)String attids)throws Exception{
		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjProposalService.suggestAdd(proposal,token,attids);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	/**
	 * 审计建议书-附件列表
	 */
	@GetMapping("/sjzj/audit_suggest_file_list")
	@ApiOperation(value = "审计建议书-附件列表")
	public JsonBean audit_suggest_file_list(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "proid", value = "业务主键", required = true) @RequestParam(value = "proid", required = true) Integer proid) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblAttachmentService.auditSuggestFileList(token,proid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
     * 审计建议书-附件删除
     */
    @GetMapping("/sjzj/audit_suggest_file_del")
    @ApiOperation(value = "审计建议书-附件删除")
    public R audit_suggest_file_del(HttpServletRequest request, HttpServletResponse response,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
    	return this.tblNbsjProposalService.removeAttInfoByAttId(token, attId);
    }
    
	
	/**
	 * 审计建议书-作废
	 */
	@GetMapping("/sjzj/audit_suggest_cancel")
	@ApiOperation(value = "审计建议书-作废")
    public JsonBean audit_suggest_cancel(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="proid",value="主键",required=true)@RequestParam(value = "proid", required = true) Integer proid) {
        
        try {
			return tblNbsjProposalService.suggestCancel(proid, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	
	/**
	 * 审计建议书-删除
	 */
	@GetMapping("/sjzj/audit_suggest_del")
	@ApiOperation(value = "审计建议书-删除")
    public JsonBean audit_suggest_del(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="proid",value="主键",required=true)@RequestParam(value = "proid", required = true) Integer proid) {
        
        try {
			return tblNbsjProposalService.suggestDelete(proid, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	
	/**
	 * 审计建议书-明细
	 */
	@GetMapping("/sjzj/audit_suggest_detail")
    @ApiOperation(value = "审计建议书-明细")
    public JsonBean audit_suggest_detail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="proid",value="主键",required=true)@RequestParam(value = "proid", required = true) Integer proid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblNbsjProposalService.findSuggestDetail(token,proid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
	
	/**
	 * 自定义报告
	 */
	@GetMapping("/nkbg/zdy_list")
	@ApiOperation(value = "自定义报告列表")
	public JsonBean zdy_list(HttpServletRequest request, TblReportVo tblReportVo,
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			tblReportVo.setType("nbsj_zdy");
			jsonBean = this.tblReportService.zdyPageList(token, pageNumber, pageSize,tblReportVo,null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
	 * 自定义报告-新增与修改
	 */
	@RequestMapping(value = "/nkbg/zdy_add", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "自定义报告-新增与修改")
    public JsonBean zdy_add(HttpServletRequest request,@ApiParam(name="zf",value="实体",required=true) TblReportEntity report,
			 				@ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
							@ApiParam(name="attids",value="附件id数组",required=false)String attids)throws Exception{
		JsonBean jsonBean = null;
		try {
			report.setType("nbsj_zdy");
			jsonBean = this.tblReportService.zdyAdd(report,token,attids);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	/**
	 * 自定义报告-删除
	 */
	@GetMapping("/nkbg/zdy_del")
	@ApiOperation(value = "自定义报告-删除")
    public JsonBean zdy_del(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="reportid",value="主键",required=true)@RequestParam(value = "reportid", required = true) Integer reportid) {
        
        try {
			return tblReportService.zdyDelete(reportid, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	/**
	 * 自定义报告-明细
	 */
	@GetMapping("/nkbg/zdy_detail")
    @ApiOperation(value = "自定义报告-明细")
    public JsonBean zdy_detail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="reportid",value="主键",required=true)@RequestParam(value = "reportid", required = true) Integer reportid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblReportService.findZdyReportDetail(token,reportid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
	
}
